package com.romainpiel.shimmer;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint.Style;
import android.graphics.Shader;
import android.os.Build;
import android.util.AttributeSet;
import android.view.animation.DecelerateInterpolator;
import android.widget.TextView;

public class TextClipView extends TextView {
	private Bitmap bitmapMask;
	private BitmapShader bitmapShader;
	private ObjectAnimator animator_tran;
	private ObjectAnimator animator_rote;
	public static final int ANIMATION_DIRECTION_LTR = 0;
	public static final int ANIMATION_DIRECTION_RTL = 1;
	private static final int DEFAULT_REPEAT_COUNT = ValueAnimator.INFINITE;
	private static final long DEFAULT_DURATION = 1800;
	private static final long DEFAULT_START_DELAY = 0;
	private static final int DEFAULT_DIRECTION = ANIMATION_DIRECTION_LTR;

	private int repeatCount = DEFAULT_REPEAT_COUNT;
	private long duration = DEFAULT_DURATION;
	private long startDelay = DEFAULT_START_DELAY;
	private int direction = DEFAULT_DIRECTION;

	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	public TextClipView(Context context, AttributeSet attrs) {
		super(context, attrs);
		bitmapMask = BitmapFactory
				.decodeResource(getResources(), R.drawable.ic);
		bitmapShader = getBitmapShader(bitmapMask);
		linearGradientMatrix = new Matrix();
		getPaint().setShader(bitmapShader);

		animator_tran = ObjectAnimator.ofInt(this, "gradientX", 0, 100);
		animator_tran.setRepeatCount(repeatCount);
		animator_tran.setDuration(duration);
		animator_tran.setStartDelay(startDelay);
		animator_tran.setRepeatMode(ObjectAnimator.REVERSE);
		animator_tran.setInterpolator(new DecelerateInterpolator());
		animator_tran.addUpdateListener(new AnimatorUpdateListener() {

			@Override
			public void onAnimationUpdate(ValueAnimator animation) {
				TextClipView.this.invalidate();
			}
		});
		animator_rote = ObjectAnimator.ofInt(this, "rote", 0, 360);
		animator_rote.setRepeatCount(repeatCount);
		animator_rote.setDuration(duration);
		animator_rote.setStartDelay(startDelay);
		animator_rote.setInterpolator(new DecelerateInterpolator());
		animator_rote.addUpdateListener(new AnimatorUpdateListener() {
			
			@Override
			public void onAnimationUpdate(ValueAnimator animation) {
			}
		});

		startAnimation();
	}
	private int rote =0;
	public void startAnimation() {
		animator_tran.start();
		animator_rote.start();
	}

	private static BitmapShader getBitmapShader(Bitmap bitmap) {
		BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP,
				Shader.TileMode.CLAMP);
		return shader;
	}

	private Matrix linearGradientMatrix;
	private int gradientX = 50;

	@Override
	protected void onDraw(Canvas canvas) {
		// canvas.drawText("哈哈", 0, 100, getPaint());
		getPaint().setStyle(Style.STROKE);
		getPaint().setStrokeWidth((float) getWidth() / 4);
		canvas.save();
		canvas.rotate(rote,getWidth() / 2, getHeight() / 2 + getWidth() / 4);
		canvas.drawCircle(getWidth() / 2, getHeight() / 2 + getWidth() / 4,
				getWidth() / 4, getPaint());
		
		canvas.restore();
		System.out.println(rote);
		
		// translate the shader local matrix
		linearGradientMatrix.setTranslate(2 * gradientX, 2 * gradientX);
		// this is required in order to invalidate the shader's position
		bitmapShader.setLocalMatrix(linearGradientMatrix);
		super.onDraw(canvas);
	}

	public int getGradientX() {
		return gradientX;
	}

	public void setGradientX(int gradientX) {
		this.gradientX = gradientX;
	}

	public int getRote() {
		return rote;
	}

	public void setRote(int rote) {
		this.rote = rote;
	}
}
